#include "BouncingBalls.h"

using namespace YoghurtGum;

struct Ball
{
	int x, y;
	float speed;
	float accel;
};

#define TOTALBALLS 32

Sprite* sprBall;
Ball Balls[TOTALBALLS];
Surface* background;

void Tests()
{
	Log::GetSingleton() << "$time VERSUS $time2" << "\n";
	Log::GetSingleton() << Date::Format("$time") << "\n";
	Log::GetSingleton() << Date::Format("$time2") << "\n";

	int total = 100000;

	DWORD start1 = GetTickCount();
	for (int i = 0; i < total; i++)
	{
		Date::Format("$time");
	}
	DWORD total1 = GetTickCount() - start1;

	Log::GetSingleton() << "First: " << total1 << " ms \n";

	DWORD start2 = GetTickCount();
	for (int i = 0; i < total; i++)
	{
		Date::Format("$time2");
	}
	DWORD total2 = GetTickCount() - start2;

	Log::GetSingleton() << "Second: " << total2 << " ms \n";
}

void BouncingBalls::Init()
{
	//Tests();

	background = new Surface("background.bmp");
	
	sprBall = new Sprite(32, 32, "test.bmp");
	for (int i = 0; i < TOTALBALLS; ++i)
	{
		Balls[i].x = (int)Random(16, Device::GetWidth() - 16);
		Balls[i].y = (int)Random(32, Device::GetHeight() - 32);
		Balls[i].speed = 0.f;
		Balls[i].accel = 0.5f;
	}
}

void BouncingBalls::Update()
{
	background->CopyTo(Device::GetDDDrawSurface(), 0, 0);

	for (int i = 0; i < TOTALBALLS; ++i)
	{
		//Balls[i].accel		+= 0.001f;
		Balls[i].speed		+= Balls[i].accel;
		Balls[i].y			+= Balls[i].speed;

		if (Balls[i].y > Device::GetHeight() - 32)
		{
			Balls[i].y = Device::GetHeight() - 32;
			Balls[i].speed *= -1.f;
		}

		sprBall->Draw(Balls[i].x, Balls[i].y);
	}
}